package com.ncs.cns.business.dao.impl;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.ncs.cns.business.dao.IMessageDao;
import com.ncs.cns.business.db.DBHelper;
import com.ncs.cns.business.to.Message;
import com.ncs.cns.framework.ahibernate.dao.impl.BaseDAO;

public class MessageDao extends BaseDAO<Message> implements IMessageDao {

	public MessageDao(Context context) {
		super(new DBHelper(context));
	}


	public List<Message> findMessageByEventId(String eventId) {
		List<Message> list = new ArrayList<Message>();
		SQLiteDatabase db = null;
		Cursor cursor = null;
		try {
			db = this.dbHelper.getReadableDatabase();
			cursor = db.rawQuery("select * from " + this.tableName +" where eventId = '"+eventId+ "' order by "+" updatedDate "+" desc",
					null);
			getListFromCursor(list, cursor);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (cursor != null) {
				cursor.close();
			}
			if (db != null) {
				db.close();
			}
		}
		return list;
	}
	
	public Message getLastMessage(String messageId,String eventId){
		List<Message> list = new ArrayList<Message>();
		SQLiteDatabase db = null;
		Cursor cursor = null;
		int count = this.getMessageCountByEventId(eventId);
		if(count == 0)return null;
		try {
			db = this.dbHelper.getReadableDatabase();
			cursor = db.rawQuery("select * from " + this.tableName +" where eventId = '"+eventId+ "' order by "+" updatedDate "+" desc" +" limit" +1+" offset " +(count -1),
					null);
			getListFromCursor(list, cursor);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (cursor != null) {
				cursor.close();
			}
			if (db != null) {
				db.close();
			}
		}
		return list.get(0);
		
	}	
	
	public List<Message> getNewMessage(int index,String eventId){
		List<Message> list = new ArrayList<Message>();
		SQLiteDatabase db = null;
		Cursor cursor = null;
		try {
			db = this.dbHelper.getReadableDatabase();
			cursor = db.rawQuery("select * from " + this.tableName +" where eventId = '"+eventId+ "' order by "+" updatedDate "+" desc" +" limit (select count(0) from "+this.tableName+") offset "+index,
					null);
			getListFromCursor(list, cursor);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (cursor != null) {
				cursor.close();
			}
			if (db != null) {
				db.close();
			}
		}
		return list;
	}
	
	public int getMessageCountByEventId(String eventId){
		int count = 0;
		SQLiteDatabase db = null;
		Cursor cursor = null;
		try {
			db = this.dbHelper.getReadableDatabase();
			cursor = db.rawQuery("select count(0) from " + this.tableName+" where eventId = '"+eventId+ "' order by "+" updatedDate "+" desc",null);			
			cursor.moveToFirst();
			count = cursor.getInt(0);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (cursor != null) {
				cursor.close();
			}
			if (db != null) {
				db.close();
			}
		}
		return count;
	}
	
}
